import wepy from 'wepy'
export default class extends wepy.mixin {
	data = {
		user: '',
		userId: '',
		// 默认地址
		getaddressDeftInfo: '',
		show: false,
		jifen: '',
		moneys: 0,
		kaquanp: 0,
		urls: wepy.$instance.globalData.baseURl,
		icon1:'2020-11-19/202011191007441605751664183_i5q9r.png',
		icon2:'2020-11-19/202011191007591605751679892_z0a0n.png',
		icon3:'2020-11-19/202011191007321605751652699_l7r7t.png'
	}
	methods = {
		// 跳转到个人信息页面
		editUser() {
			console.log('个人信心页面')
			wepy.navigateTo({
				url: '../../pages/editUser/edituser?stated='+1,
			})
		},
		// 跳转到订单
		handOrder(i) {
			wepy.navigateTo({
				url: '/pages/Order/order?i=' + i,
			})
		},
		// 跳转到收藏
		goLike() {
			wepy.navigateTo({
				url: '../../pages/goodsLike/goodslike',
			})
		},
		// 跳转到反馈建议
		goadvice() {
			console.log('跳转到反馈建议')
			wepy.navigateTo({
				url: '../../pages/Advice/advice',
			})
		},
		// 跳转到常见问题
		goQuestion() {
			console.log('跳转到反馈问题')

			wepy.navigateTo({
				url: '../../pages/oftenQuestion/oftenquestion',
			})
		},
		phone() {
			this.getHotList()
		},
		// 新增地址
		addreess() {
			wepy.navigateTo({
				url: '../../pages/shouhuolist/shouhuolist',
			})
		},
		//应缴物业费
		payhouseMoney() {
			wepy.navigateTo({
				url: '../../pages/payHouseMoney/payhousemoney',
			})
		},
		//R币
		chongzhi() {
			wepy.navigateTo({
				url: '../../pages/chongzhi/chongzhi',
			})
		},
		//积分历史
		integral() {
			wepy.navigateTo({
				url: '../../pages/integral/integral',
			})
		},
		//卡券
		coupon() {
			wepy.navigateTo({
				url: '../../pages/coupon/coupon',
			})
		},
		//客服
		onChange(event) {
			this.activeNames = event.detail
		},
		onClickShow() {
			console.log("9999999999")
			this.show = true;
		},
		onClickHide() {
			this.show = false;
		},
		address() {
			wepy.navigateTo({
				url: '../../pages/shouhuolist/shouhuolist?stated='+1
			});
		},
		//物业客服
		kefu() {
			wepy.navigateTo({
				url: '../../pages/keFu/kefu',
			})
		},
		// 修改手机号
		edittelnum() {
			console.log('修改手机号')
			wepy.navigateTo({
				url: '../../pages/Editphone/editphone?state=' + 1,
			})
		},
		getPhoneNumber(e) {
			var that=this;
			if (e.detail.errMsg == "getPhoneNumber:fail user deny") { //拒绝
				return wepy.noToast('您拒绝了获取您的手机号');
			} else { //允许
				console.log(e.detail)
				wx.checkSession({
					success() {
						that.hqsjh(e.detail)
						//session_key 未过期，并且在本生命周期一直有效
					},
					fail() {
						wx.navigateTo({
							url: '../../pages/Authorization/authorization',
						})
						// session_key 已经失效，需要重新执行登录流程
					}
				})
			}
		},
		shezhi(){
			wepy.navigateTo({
				url: '../../pages/setup/setup',
			})
		}
	}
	onLoad() {
		
	}
	onShow() {
		if (wepy.getStorageSync('userId')) {
			this.cartId = [];
			this.shopnum = [];
			this.user = wx.getStorageSync('user')
			let userid = wepy.getStorageSync('userId')
			this.userId = userid
			this.getAddressdefalt()
			this.jifenlist()
			this.kaquan()
		} else {
			wx.navigateTo({
				url: '../../pages/Authorization/authorization',
			})
		}
	}
	//获取手机号
	async hqsjh(e) {
		const {
			data: res
		} = await wepy.post('baseRest/analyseMobile', {
			encryptedData: e.encryptedData,
			iv: e.iv,
			sessionKey:wepy.getStorageSync('session_key')
		});
		console.log(res)
		this.phoneds(res.purePhoneNumber)
	}
	//保存手机号
	async phoneds(memberPhone) {
		const {
			data: res
		} = await wepy.post('baseRest/setMemberPhoneWithNoCode', {
			memberId: wepy.getStorageSync('userId'),
			memberPhone: memberPhone
		});
		if (res.state != 1) {
			return wepy.noToast('暂无数据');
		} else {
			wepy.navigateTo({
				url: '../../pages/setup/setup',
			})
		}
	}
	async kaquan() {
		const {
			data: res
		} = await wepy.post('baseRest/userCardList', {
			memberId: wepy.getStorageSync('userId'),
			pageIndex: "1",
			pageSize: "100",
			type: "unsed"
		});
		if (res.state != 1) {
			return wepy.noToast('暂无数据');
		} else {
			this.kaquanp = res.data.length;
			console.log(res.data)
			this.$apply()
		}
	}
	// 缴费合计
	async heji(id) {
		const res = await wepy.post('baseRest/getSumByMemberIdAndHouseId', {
			memberId: wepy.getStorageSync('userId'),
			houseId: id
		});
		if (res.data.state == 0) {
			wepy.noToast(res.data.message);
		} else {
			console.log(res.data, "合计金额")
			this.moneys = Number(res.data.data.sum);
			this.$apply()
		}
	}
	// 获取积分
	async jifenlist() {
		const res = await wepy.post('baseRest/getMemberRCoinAndJf', {
			memberId: wepy.getStorageSync('userId'),
		})
		console.log(res, "积分")
		if (res.statusCode != 200) {
			return wepy.noToast('获取数据失败')
		}
		this.jifen = res.data.data
		this.$apply()
	}
	// 获取默认地址
	async getAddressdefalt() {
		const res = await wepy.post('baseRest/getDefaultAddr', {
			memberId: this.userId,
		})
		console.log('获取默认地址', res)
		if (res.statusCode != 200) {
			wepy.noToast('获取地址失败')
		}
		if (res.data.data.area) {
			this.getaddressDeftInfo = res.data.data
			this.heji(res.data.data.id)
		} else {
			this.getaddressDeftInfo = ''
		}
		this.$apply()
	}
	// 客服
	async getHotList() {
	  const { data: res } = await wepy.post('baseRest/getDictionary', {
	    id: '103'
	  })
	  console.log('客服', res)
	  if(res.state != 1) {
	    return wepy.noToast(res.message)
	  }
	  wepy
	  	.makePhoneCall({
	  		phoneNumber: res.data,
	  	})
	  	.catch((err) => console.log('用户点击了取消'))
	}
}
